﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using KZ.Exception;
using KZ.Lottery.BLL;
using KZ.Lottery.Entities;
using KZ.Lottery.Resources;
using KZ.Utilities;

namespace KZ.Lottery
{
    public partial class ucRuleDetailGroup : UserControl
    {
        #region Private Variables

        string _RuleDetailID;
        decimal _HeadCapital, _TailCapital;
        Dictionary<string, decimal> _HeadValueList, _TailValueList;
        Dictionary<string, decimal> _HeadFirstValueList, _TailFirstValueList;
        List<RuleDetailGroup> _RuleDetailGroupList;

        #endregion Private Variables

        #region Private Properties

        private Dictionary<string, decimal> HeadValueList
        {
            set
            {
                int count = value.Count;
                string temp;
                decimal nextCapital = 0;
                for (int i = 0; i < count; i++)
                {
                    temp = i.ToString("00");
                    nextCapital += value[temp] - FirstDay * Global.NumberOfValue;
                    dtgHeadValue[colHeadValue.Index, i].Value = temp;
                    dtgHeadValue[colHeadValueAmount.Index, i].Value = Utility.CurrencyParse(value[temp], Global.Culture);
                    dtgHeadValue[colHeadCapital.Index, i].Value = Utility.CurrencyParse(value[temp] - _HeadFirstValueList[temp], Global.Culture);
                }

                txtHeadNextCapital.Text = Utility.CurrencyParse(nextCapital, Global.Culture);
                txtHeadNextActualCapital.Text = Utility.CurrencyParse(nextCapital * Global.CapitalRate / 100, Global.Culture);
            }
        }

        private Dictionary<string, decimal> TailValueList
        {
            set
            {
                int count = value.Count;
                string temp;
                decimal nextCapital = 0;
                for (int i = 0; i < count; i++)
                {
                    temp = i.ToString("00");
                    nextCapital += value[temp] - FirstDay * Global.NumberOfValue;
                    dtgTailValue[colTailValue.Index, i].Value = temp;
                    dtgTailValue[colTailValueAmount.Index, i].Value = Utility.CurrencyParse(value[temp], Global.Culture);
                    dtgTailValue[colTailCapital.Index, i].Value = Utility.CurrencyParse(value[temp] - _TailFirstValueList[temp], Global.Culture);
                }

                txtTailNextCapital.Text = Utility.CurrencyParse(nextCapital, Global.Culture);
                txtTailNextActualCapital.Text = Utility.CurrencyParse(nextCapital * Global.CapitalRate / 100, Global.Culture);
            }
        }

        #endregion

        #region Private Methods

        private void ucRuleDetailGroup_Load(object sender, EventArgs e)
        {
            _HeadValueList = new Dictionary<string, decimal>();
            _TailValueList = new Dictionary<string, decimal>();
            _HeadFirstValueList = new Dictionary<string, decimal>();
            _TailFirstValueList = new Dictionary<string, decimal>();
            _RuleDetailGroupList = new List<RuleDetailGroup>();

            LoadRuleDetail();
            LoadRuleDetailGroup();
            tabControl1_SelectedIndexChanged(tabControl1, null);
        }

        private void LoadRuleDetail()
        {
            try
            {
                RuleDetail ruleDetail = RuleDetailBLL.GetRuleDetailByID(_RuleDetailID);

                txtHeadActualCapital.Text = Utility.CurrencyParse(ruleDetail.HeadActualCapital, Global.Culture);
                txtTailActualCapital.Text = Utility.CurrencyParse(ruleDetail.TailActualCapital, Global.Culture);
                txtHeadCapital.Text = Utility.CurrencyParse(ruleDetail.HeadCapital, Global.Culture);
                txtTailCapital.Text = Utility.CurrencyParse(ruleDetail.TailCapital, Global.Culture);
                txtHeadWinAmount.Text = Utility.CurrencyParse(ruleDetail.HeadWinAmount, Global.Culture);
                txtTailWinAmount.Text = Utility.CurrencyParse(ruleDetail.TailWinAmount, Global.Culture);
                txtHeadRevenue.Text = Utility.CurrencyParse(ruleDetail.HeadWinAmount - ruleDetail.HeadActualCapital, Global.Culture);
                txtTailRevenue.Text = Utility.CurrencyParse(ruleDetail.TailWinAmount - ruleDetail.TailActualCapital, Global.Culture);

                if (!String.IsNullOrEmpty(ruleDetail.Head) || !String.IsNullOrEmpty(ruleDetail.Tail))
                {
                    HeaderText = ruleDetail.ChannelName;
                    txtHeadResultDate.Text = ruleDetail.ResultDate.ToShortPattern(Global.Culture);
                    txtTailResultDate.Text = ruleDetail.ResultDate.ToShortPattern(Global.Culture);
                    txtHeadResult.Text = ruleDetail.Head;
                    txtTailResult.Text = ruleDetail.Tail;
                }
                else
                {
                    HeaderText = RuleDetailChannelBLL.GetRuleDetailChannelByNo(_RuleDetailID);
                }
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadRuleDetail", ex);
                throw new BusinessLogicException(KZ_Message.BLM_ERR_GENERAL);
            }
        }

        private void LoadRuleDetailGroup()
        {
            try
            {
                string ruleDetailID = "'" + _RuleDetailID + "'";
                _RuleDetailGroupList = RuleDetailGroupBLL.GetRuleDetailGroupList(ruleDetailID);
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadRuleDetailGroup", ex);
                throw new BusinessLogicException(KZ_Message.BLM_ERR_GENERAL);
            }
        }

        private void LoadHeadValue()
        {
            try
            {
                dtgHeadValue.Rows.Clear();
                dtgHeadValue.Rows.Add(100);
                _HeadValueList.Clear();
                _HeadFirstValueList.Clear();
                for (int i = 0; i < 100; i++)
                {
                    _HeadValueList.Add(i.ToString("00"), 0);
                    _HeadFirstValueList.Add(i.ToString("00"), 0);
                }
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadHeadValue", ex);
                throw new BusinessLogicException(KZ_Message.BLM_ERR_GENERAL);
            }
        }

        private void LoadTailValue()
        {
            try
            {
                dtgTailValue.Rows.Clear();
                dtgTailValue.Rows.Add(100);
                _TailValueList.Clear();
                _TailFirstValueList.Clear();
                for (int i = 0; i < 100; i++)
                {
                    _TailValueList.Add(i.ToString("00"), 0);
                    _TailFirstValueList.Add(i.ToString("00"), 0);
                }
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadTailValue", ex);
                throw new BusinessLogicException(KZ_Message.BLM_ERR_GENERAL);
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedTab.Name == tbpHead.Name)
            {
                LoadHeadValue();
                LoadRuleDetail();
                LoadHeadRuleDetailGroup();
            }
            else
            {
                LoadTailValue();
                LoadRuleDetail();
                LoadTailRuleDetailGroup();
            }
        }

        private void LoadHeadRuleDetailGroup()
        {
            try
            {
                int tempCount;
                int count = _RuleDetailGroupList.Count;

                dtgHead.Rows.Clear();
                dtgHead.Rows.Add(count);
                List<GroupValue> groupValueList;
                _HeadCapital = 0;
                for (int i = 0; i < count; i++)
                {
                    dtgHead[colHeadOrder.Index, i].Value = _RuleDetailGroupList[i].GroupID;
                    dtgHead[colHeadDay.Index, i].Value = _RuleDetailGroupList[i].HeadDay;
                    dtgHead[colHeadAmount.Index, i].Value = _RuleDetailGroupList[i].HeadAmount;

                    _HeadCapital += _RuleDetailGroupList[i].HeadDay * Global.NumberOfGroup;

                    groupValueList = new List<GroupValue>(GroupValueList.Where(gv => gv.GroupID == _RuleDetailGroupList[i].GroupID));
                    tempCount = groupValueList.Count;
                    for (int j = 0; j < tempCount; j++)
                    {
                        _HeadValueList[groupValueList[j].Value] += _RuleDetailGroupList[i].HeadAmount;
                        _HeadFirstValueList[groupValueList[j].Value] += _RuleDetailGroupList[i].FirstDayAmount;
                    }
                }

                HeadValueList = _HeadValueList;
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadHeadRuleDetailGroup", ex);
                throw new BusinessLogicException(KZ_Message.BLM_ERR_GENERAL);
            }
        }

        private void LoadTailRuleDetailGroup()
        {
            try
            {
                int tempCount;
                int count = _RuleDetailGroupList.Count;
                dtgTail.Rows.Clear();
                dtgTail.Rows.Add(count);
                List<GroupValue> groupValueList;
                _TailCapital = 0;
                for (int i = 0; i < count; i++)
                {
                    dtgTail[colTailOrder.Index, i].Value = _RuleDetailGroupList[i].GroupID;
                    dtgTail[colTailDay.Index, i].Value = _RuleDetailGroupList[i].TailDay;
                    dtgTail[colTailAmount.Index, i].Value = _RuleDetailGroupList[i].TailAmount;

                    _TailCapital += _RuleDetailGroupList[i].TailDay * Global.NumberOfGroup;

                    groupValueList = new List<GroupValue>(GroupValueList.Where(gv => gv.GroupID == _RuleDetailGroupList[i].GroupID));
                    tempCount = groupValueList.Count;
                    for (int j = 0; j < tempCount; j++)
                    {
                        _TailValueList[groupValueList[j].Value] += _RuleDetailGroupList[i].TailAmount;
                        _TailFirstValueList[groupValueList[j].Value] += _RuleDetailGroupList[i].FirstDayAmount;
                    }
                }

                TailValueList = _TailValueList;
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadTailRuleDetailGroup", ex);
                throw new BusinessLogicException(KZ_Message.BLM_ERR_GENERAL);
            }
        }

        #endregion Private Methods

        #region Constructors

        public ucRuleDetailGroup()
        {
            InitializeComponent();

            colHeadAmount.DefaultCellStyle.Format = "N" + Global.Culture.NumberFormat.CurrencyDecimalDigits;
            colTailAmount.DefaultCellStyle.Format = "N" + Global.Culture.NumberFormat.CurrencyDecimalDigits;
        }

        #endregion Constructors

        #region Public Properties

        public string RuleDetailID
        {
            set { _RuleDetailID = value; }
        }

        public string HeaderText { get; set; }

        public decimal FirstDay { get; set; }

        public List<GroupValue> GroupValueList { get; set; }

        #endregion Public Properties
    }
}